package com.cwj.designpatterns.simplefactory;

/**
 * @author chenwujie
 * @date 2021-03-15 09:09
 */
public class SimpleFactory {

    public static void main(String[] args) {
        Fruit fruit = SimpleFactory.create(Apple.class);
        fruit.printName();
        fruit = SimpleFactory.create(Banana.class);
        fruit.printName();
    }
    public static Fruit create(Class<? extends Fruit> cls) {
        if (cls != null) {
            try {
                return cls.newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
